﻿using System;
using System.Collections.Generic;

namespace Simhopp.Common
{
    public static class GenericMap
    {
        public static int SaveObjectToDatabase(IDatabaseObject obj)
        {
            if (obj == null)
            {
                MyLogger.Error("Null-object passed to Save-database function");
                return -1;
            }
            try
            {
                var getObjectFromDb = MyDbConnection.ReadFromTable(String.Format("select * from {0} where {1}={2}", obj.GetTableName(), obj.GetIdFieldName(), obj.Id));
                if (getObjectFromDb.Count == 0) // New Object;
                {
                    var insertString = String.Format("insert into {0} {1}", obj.GetTableName(), GenerateSqlQuerys.InsertQuery(obj.GetDictionary(), obj.GetIdFieldName()));
                    obj.Id = MyDbConnection.ExecuteInsert(insertString);
                    MyLogger.Debug(String.Format("{0} {1} saved", obj.GetType(), obj.Id));
                }
                else // Update
                {
                    var updateString = String.Format("update {0} set {1}", obj.GetTableName(), GenerateSqlQuerys.UpdateQuery(obj.GetDictionary(), obj.GetIdFieldName()));
                    MyDbConnection.ExecuteUpdate(updateString);
                    obj.Id = obj.Id;
                    MyLogger.Debug(String.Format("{0} {1} saved", obj.GetType(), obj.Id));
                }
                return obj.Id;
            }
            catch (Exception e)
            {
                MyLogger.Error(String.Format("Could not save {0} {1} to database - {2}", obj.GetType(), obj.Id, e.Message));
                return -1;
            }
        }

        public static Dictionary<String, Object> ReadObjectFromDatabase(int id, Type t)
        {
            var obj = (IDatabaseObject)Activator.CreateInstance(t);
            var result = MyDbConnection.ReadFromTable(String.Format("select * from {0} where {1}='{2}'", obj.GetTableName(), obj.GetIdFieldName(), id));
            return result.Count > 0 ? result[0] : null;
        }

        public static List<Dictionary<string, object>> ReadAllObjectsFromDatabase(Type type)
        {
            try
            {
                var obj = (IDatabaseObject)Activator.CreateInstance(type);
                var sqlResult = MyDbConnection.ReadFromTable(String.Format("select * from {0}", obj.GetTableName()));
                MyLogger.Debug(String.Format("All objects of type {0} succesfully read from database", type.Name));
                return sqlResult;
            }
            catch (Exception e)
            {
                MyLogger.Error(String.Format("Cannot read all objects of type {0} from database - {1}", type.Name,  e.Message));
                return null;
            }
        }

        public static void RemoveObjectFromDatabase(IDatabaseObject obj)
        {
            var deleteString = String.Format("delete from {0} {1}", obj.GetTableName(), GenerateSqlQuerys.DeleteQuery(obj.GetDictionary(), obj.GetIdFieldName()));
            var res = MyDbConnection.ExecuteUpdate(deleteString);
            if (res)
                MyLogger.Debug(String.Format("Object {0} of type {1} successfully removed from database", obj.Id,
                    obj.GetType()));
            else
                MyLogger.Error(String.Format("Object {0} of type {1} could not be removed from database", obj.Id,
                    obj.GetType()));
        }
    }
}
